package com.nmgydy.util;

import java.io.*;
import java.util.Properties;

public class ConfigUtil {

	private static Properties props;

	static {
		// props = readProperties(getWebPath() + fileName);
		InputStream fis = null;
		try {
			// 初始化路径
			String fileName = "config.properties";
			String filePath = getConfigFolderPath() + fileName;
			// 读取属性文件
			props = new Properties();
			fis = new FileInputStream(filePath);
			BufferedReader bf = new BufferedReader(new InputStreamReader(fis));
			props.load(bf);
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			try {
				if (fis != null) {
					fis.close();
				}
			} catch (IOException e) {
			}
		}
	}

	private ConfigUtil() {
	}

	/** 获取配置文件夹的路径 */
	private static String getConfigFolderPath() {
		StringBuilder sb = new StringBuilder();
		String webPath = System.getProperty("WebRoot");
		if (webPath == null) {
			webPath = ConfigUtil.class.getResource("/").getPath();
			sb.append(webPath);
		} else {
			sb.append(webPath);
			sb.append("WEB-INF").append(File.separator);
			sb.append("classes").append(File.separator);
		}
		return sb.toString();
	}

	/**
	 * 根据给定的key获取:
	 * @param key
	 * @return
	 */
	public static String getValue(String key) {
		if(props.getProperty(key) != null){
			return props.getProperty(key).trim();
		}
		return "";
	}

	/**
	 * 获取int值
	 */
	public static int getIntValue(String key) {
		return Integer.parseInt(getValue(key));
	}
	
	public static boolean getBooleanValue(String key) {
		return Boolean.parseBoolean(getValue(key));
	}
	
	public static void main(String[] args) {
		System.out.println(getValue("daily.loginUrl"));
	}
}